<?php

/*
 *
 */

class Model_Mediate_PublicCase extends Model_BaseModel
{
    use Lib_BaseTraits;
    /**
     * 缓存前缀
     *
     * @var unknown
     */
    private static $tag = 'Model_Mediate_PublicCase';
    /**
     * Undocumented variable
     *
     * @var string
     */
    private $table = "public_case";

    public function __construct()
    {
        // 选择连接的数据库
        parent::_init('mediation');
    }
    public function updateStatusByImportId($recordId,$status,$whereStatus){
        if($whereStatus === ''){
            $whereStatus = '1,2';
        }
        $sql =  "UPDATE `public_case` SET caseStatus = :caseStatus, updateTime = :updateTime where importRecordId = :importRecordId and caseStatus in ($whereStatus)";
        $res = $this->dao->conn(false)->noCache()->preparedSql($sql, [
            ':caseStatus' => $status,
            ':updateTime' => time(),
            ':importRecordId' => $recordId,
        ])->affectedCount();
        $this->dao->clearTag(self::$tag);
        return $res;
    }
    public function updateNumByImportId($recordId,$mediationNum){
        $y = date('Y');
        $sql =  "UPDATE `public_case` SET mediationNum = CONCAT('渝（{$y}）{$mediationNum}',$recordId,'-',id,'号') where importRecordId = :importRecordId and caseStatus = 1 and mediationNum = ''";
        $res = $this->dao->conn(false)->noCache()->preparedSql($sql, [
            ':importRecordId' => $recordId,
        ])->affectedCount();
        $this->dao->clearTag(self::$tag);
        return $res;
    }
    public function saveByIds($updateDta, $ids, $where = [])
    {
        $data = [];
        $sql = 'UPDATE `public_case` SET ';
        $sqlarr = array();
        foreach ($updateDta as $key => $value) {
            if(strstr($value,'CONCAT(')){
                array_push($sqlarr, "`{$key}`={$value}");
            }else{
                array_push($sqlarr, "`{$key}`=:$key");
                $data[":$key"] = $value;
            }
        }
        $where_str = '';
        if (!empty($where)) {
            $where_str = 'AND '.implode(' AND ', $where);
        }
        $sql .= implode(',', $sqlarr);
        $sql .= " WHERE id in ({$ids}) {$where_str}";
        $res = $this->dao->conn(false)->noCache()->preparedSql($sql, $data)->affectedCount();
        $this->dao->clearTag(self::$tag);
        return $res;
    }

}
